<?php
global $m, $tab, $a;
$perm = &$AppUI->acl();
if (!$perm->userHasRole($AppUI->user_id, 'Accounting')) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect("m=home&tab=0");
	exit;
}
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getModuleClass('companies'));
require_once($AppUI->getModuleClass('contacts'));
require_once($AppUI->getMasterDataClass('mdcurrency'));

$currencyArr = getCurrencyList('currency_abbreviation');
$df = $AppUI->getPref('SHDATEFORMAT');
$cDate = new CDate();
$customer = intval( dPgetParam( $_REQUEST, 'project_company', 0 ) );

$cus = new CCompany();
$cus->load($customer);

$q = new DBQuery();
$q->addTable('contacts');
$q->addWhere("contact_company = $customer");
$q->addWhere("contact_type = 'Attn'");
$q->loadObject($contact);

$pro = new CProject();
$totalRows = 0;
$_REQUEST['expense_type'] = 2;
$projects = $pro->getList($_REQUEST, $totalRows);

$sumOnBehalf_VND = 0;
$sumOnBehalf_USD = 0;
$sumAR_USD = 0;
$sumAR_VND = 0;
$exchange = dPgetParam( $_REQUEST, CURRENCY_USD_VALUE . "_vnd", 1 );
foreach ($projects as $row) {
	$i++;
	$currency = $row['project_currency'];
//	$t = array($row['project_iso_code'], $currency);
//	$onbehalf = $pro->getExpenseAmount($row['project_id'], 2);
	$onbehalf = $row['ea_sum'];

	$sumOnBehalf_VND += $onbehalf;
//	$sumOnBehalf_USD += $onbehalf / $exchange;

	$ar = $row['sa_sales_amount'] + $row['sa_vat_amount'];
	if ($currency == CURRENCY_VND_VALUE) {
		$sumAR_VND += $ar;
//		$sumAR_USD += $ar / $exchange;
	} else {
		$sumAR_USD += $ar;
		$sumAR_VND += $ar * $exchange;
	}

}
?>
<link rel="stylesheet" type="text/css" href="style/default/bamboo.css" media="all" />
<link rel="stylesheet" type="text/css" href="style/default/invoice.css" media="all" />
<link rel="stylesheet" type="text/css" href="style/default/bamboo_print.css" media="print" />
<div class="invoiceViewHold">
	<div id="companyDetails">
	  <table>
	    <tr>
	      <td rowspan="2"><img src="/images/dragon.png" border="0" width="110" height="100"/></td><td><h2>Dragon Logistics Co., Ltd</h2></td>
	    </tr>
	    <tr>
	      <td><p>E-4A Thang Long Industrial Park, Dong Anh District,<br/> Ha Noi, Viet Nam<br/><a href="http://www.dragonlogistics.com">http://www.dragonlogistics.com</a></p></td>
	    </tr>
	  </table>
	</div>
  <table style="font-weight:bold" class="address">
    <tr>
      <td>Debit Note No</td><td>:</td><td><?=$cus->company_code . '-' . $cDate->format('%m/%y')?></td>
    </tr>
    <tr>
      <td>Date of Issue</td><td>:</td><td><?=$cDate->format('%B %d, %Y')?></td>
    </tr>
    <tr>
      <td>Our Invoice No</td><td>:</td><td><?=dPgetParam($_REQUEST, 'debit_invoice', '')?></td>
    </tr>
  </table>
<hr/>
<h3><?=$cus->company_name?></h3>
<table >
    <tr>
      <td><p>Address</p></td><td><p>:</p></td><td><p><?=$cus->company_address1?></p></td>
    </tr>
    <tr>
      <td><p>VAT code</p></td><td><p>:</p></td><td><p><?=$cus->company_vat_code?></p></td>
    </tr>
    <tr>
      <td><p>Attn</p></td><td><p>:</p></td><td><p><?=$contact->contact_order_by?></p></td>
    </tr>
  </table>
  <h4>Work description</h4>
	<table class="invoice_items stripe">
		<tbody>
		<tr class="even">
			<th>NO</th>
			<th style="text-align:left">Description</th>
			<th style="text-align:right">Amount USD</th>
			<th style="text-align:right">Amount VND</th>
		</tr>
		<tr>
			<td style="text-align:center"><p>1</p></td>
			<td><p>Handling and transportation fee</p></td>
			<td style="text-align:right"><p><?=currencyFormat($sumAR_USD, FALSE)?></p></td>
			<td style="text-align:right"><p><?=currencyFormat($sumAR_VND, TRUE)?></p></td>
		</tr>
		<tr class="even">
			<td style="text-align:center"><p>2</p></td>
			<td><p>Another onbehalf charge</p></td>
			<td style="text-align:right"><p>&nbsp;</p></td>
			<td style="text-align:right"><p><?=currencyFormat($sumOnBehalf_VND, TRUE)?></p></td>
		</tr>
		<tr>
			<th>&nbsp;</th>
			<th style="text-align:right">Total</th>
			<th style="text-align:right"><?=currencyFormat($sumAR_USD + $sumOnBehalf_USD, FALSE)?></th>
			<th style="text-align:right"><?=currencyFormat($sumAR_VND + $sumOnBehalf_VND, TRUE)?></th>
		</tr>
	</tbody></table>
	<table width="90%">
	<tr>
		<td width="50%">
			<strong>Exchange rate: <?=currencyFormat(dPgetParam( $_REQUEST, CURRENCY_USD_VALUE . "_vnd", "" ), TRUE)?></strong><br/><br/>
			<strong>Note:</strong>
			<p><?=dPgetParam($_REQUEST, 'debit_note', '')?></p>
			<strong>Please remit to:</strong><br/>
			<p>
				THE BANK OF TOKYO MITSUBISHI UFJ, LTD - Ha Noi Branch.<br/>
				83B Ly Thuong Kiet, Ha Noi, Viet Nam.<br/>
				USD Account No: 131 067<br/>
				VND Account No: 042 307<br/>
				or<br/>
				MIZUHO CORPORATE BANK, LTD - Ha Noi Branch.<br/>
				63 Ly Thai To, Ha Noi, Viet Nam.<br/>
				USD Account No: F15-795-000057<br/>
				VND Account No: H15-795-000065<br/>
			</p>
		</td>
		<td width="10%">&nbsp;<td>
		<td width="30%" valign="top" nowrap="true">
			<br/><br/><br/>
			<strong style="text-align: center; font-size: 13pt">On behalf of Dragon Logistics Co., Ltd</strong>
		</td>
	</tr>
	</tabe>
</div>